package com.pirestupppai.widgets.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

import com.pirestupppai.R;


/**
 * 加载中对话框
 *
 * @author: xiaoluo
 * @date: 2017-01-14 15:27
 */
public class ProgressingDialog extends ProgressDialog {
    private AnimationDrawable mAnimation;
    private Context mContext;
    private ImageView mImageView;
    // 动画资源id
    private int mResid;

    public ProgressingDialog(Context context) {
        super(context, R.style.ProgressDialog);

        this.mContext = context;
        this.mResid = R.drawable.anim_loading;
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    private void initData() {
        mImageView.setBackgroundResource(mResid);

        // 通过ImageView对象拿到显示的AnimationDrawable
        mAnimation = (AnimationDrawable) mImageView.getBackground();
        // 为了防止在onCreate方法中只显示第一帧的解决方案之一
        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.start();
            }
        });
    }

    private void initView() {
        setContentView(R.layout.dialog_progressing);
        mImageView = (ImageView) findViewById(R.id.loading_iv);
    }

    @Override
    public void dismiss() {
        if (this.isShowing()) {
            super.dismiss();
        }
    }
}
